package com.updalod.service.impl;

import com.updalod.service.MinIoService;
import com.updalod.util.EncodingUtil;
import com.updalod.util.MinIoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @program: minio
 * @description:
 * @author: WuChengXing
 * @create: 2020-07-16 21:08
 **/

@Service
public class MinIoServiceImpl implements MinIoService {

    private MinIoUtil minIoUtil;

    public MinIoServiceImpl(MinIoUtil minIoUtil) {
        this.minIoUtil = minIoUtil;
    }

    @Value("${file.size}")
    private Integer fileSize;

    @Value("${kkFileView.url}")
    private String kkFileViewUrl;

    @Override
    public String fileUpload(MultipartFile file) {
        if (null == file) {
            return "没有文件";
        }
        if (file.getSize() > fileSize) {
            return "文件过大";
        }
        String url = minIoUtil.fileUpload(file, "common");
        return url;
    }

    @Override
    public String customizeDirUpload(MultipartFile file, String dir) {
        if (null == file) {
            return "没有文件";
        }
        if (file.getSize() > fileSize) {
            return "文件过大";
        }
        String url = minIoUtil.fileUpload(file, dir);
        return url;
    }

    @Override
    public String onlinePreview(String url) {
        String preview = kkFileViewUrl + "/onlinePreview?url=" + EncodingUtil.encodeURIComponent(url);
        return preview;
    }
}
